<?php

class q_mvc_view {

    public $_data = array();

    public $_template = '';

    public $_controller;


    public function __set($name, $value){
        $this->_data[$name] = $value;
    }

    public function __get($name){
        return $this->_data[$name];
    }

    public function __isset($name){
        return isset($this->_data[$name]);
    }

    public function __unset($name){
        unset($this->_data[$name]);
    }

    public function setController($controller){
        $this->_controller = $controller;
    }

    public function setTemplate($tpl){
        $this->_template = $tpl;
    }

    public function out($name, $type = 'html', $default = ''){
        if(isset($this->_data[$name])){
            echo $this->escape($this->_data[$name], $type);
        } else {
            echo $this->escape($default, $type);
        }
    }

    public function escape($str, $type = 'html'){
        return htmlspecialchars($str);
    }

    public function output(){
        if(file_exists($this->_template)) {
            include $this->_template;
        } else {
            $this->_controller->error('tpl file not exists', $this->_template);
        }
    }

}

